please do

# The following script is similar to the Collect PhiPsi
# but demonstrate the use of arrays to store values in a first pass
# then how a second loop actually writes data.
# Then it will reopen the file and create a second one 
# swapping the columns phi and psi, which demonstrates how
# to parse and process text files (which could be used as
# input parameters for scripts to provide lists).


open pdb from download "1CRN.pdb";
$NBGROUPS = groupcount of "1CRN";

# ---- computing values ----.

$X = 0;
do
{
	$SEL = select in "1CRN" pos $X;
	$PHI[$X] = phi($SEL);
	$PSI[$X] = psi($SEL);
} while (++$X < $NBGROUPS);

# ---- writing values in file ----.

$MYFILENAME = "results";
$MYFILE = open file $MYFILENAME in usrstuff for writing; 
$X = 0;
do
{
	print on $MYFILE "phi= " + (string)$PHI[$X] + " psi= " + (string)$PSI[$X];
} while (++$X < $NBGROUPS);
close file $MYFILE;

# ---- reading and writing a swapped file

$MYFILENAME2 = "swappedresults";
$INPUTFILE = open file $MYFILENAME in usrstuff for reading; 
$OUTPUTFILE = open file $MYFILENAME2 in usrstuff for writing; 
do
{
	$LINE = readln from file $INPUTFILE;
	$PHItxt = substring 1 of $LINE;  # remember that fields start at zero (which contains 'phi=')
	$PSItxt = substring 3 of $LINE;
	if ($LINE != "")  #line not empty
	{
		print on $OUTPUTFILE "psi= " + $PSItxt + " phi= " + $PHItxt;
	}
} while ($LINE != "");

close file $INPUTFILE;
close file $OUTPUTFILE;


# --- showing their content -------

open text $MYFILENAME  in usrstuff;
open text $MYFILENAME2 in usrstuff;

thank you
